package mk.ukim.finki.studentguide;

import java.util.ArrayList;
import java.util.List;

import mk.ukim.finki.studentguide.xmlconfiguration.Building;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private  Context mContext = null;

	public ItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}
	
	@Override
	protected OverlayItem createItem(int arg0) {
		return mOverlays.get(arg0);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}
	
	@Override
	protected boolean onTap(int index) {
	  OverlayItem item = mOverlays.get(index);
	  GeoPoint point=item.getPoint();
	  int latitude=point.getLatitudeE6();
	  int longitude=point.getLongitudeE6();
	  StudentGuideMapActivity sgma=new StudentGuideMapActivity();
	  sgma=(StudentGuideMapActivity)mContext;
	  List<Building> lstBuildings = ((MyApplication) sgma.getApplication()).getLstBuildings();
	  for(int i=0;i<lstBuildings.size();i++){
		  if(latitude==Integer.parseInt(lstBuildings.get(i).getLatitude()) && longitude==Integer.parseInt(lstBuildings.get(i).getLongitude())){
			  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			  dialog.setTitle(item.getTitle());
			  dialog.setMessage(item.getSnippet());
			  dialog.setPositiveButton("Yes", dialogClickListener)
			    .setNegativeButton("No", dialogClickListener).show();
			  ((MyApplication) sgma.getApplication()).setSelectedBuilding(i);
		  }
	  }
      return true;
	}
	DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
	    public void onClick(DialogInterface dialog, int which) {
	        switch (which){
	        case DialogInterface.BUTTON_POSITIVE:
	        	goToShowBuilding();
	            break;

	        case DialogInterface.BUTTON_NEGATIVE:
	            break;
	        }
	    }
	};
	
	public void goToShowBuilding(){
		  StudentGuideMapActivity vmc=new StudentGuideMapActivity();
		  vmc=(StudentGuideMapActivity)mContext;
		  Intent intent=new Intent(vmc,ShowBuilding.class);
	      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	      mContext.startActivity(intent);
	}
}
